﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using HZ.Supervise.Model;

namespace HZ.Supervise.Web.Secondphase.ReportCompany
{
    public partial class CompanyGoodsDetail : BasePage
    {
        private string strgoodsid = "";//流水号
        private string strMosth = "";//月份 
        private string strcompanynameps = "";//配送企业名称
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ViewState["query"] = URLUtil.GetSuperviseQueryFromURL(Request);
                BindList();
            }
        }
        /// <summary>
        /// 绑定    by张雄  2011.5.25
        /// </summary>
        void BindList()
        {
            
            SuperviseQuery query = ViewState["query"] as SuperviseQuery;


            //query.SortSource = null;//清空SortSource

            SuperviseQuery sq = new SuperviseQuery();
            sq.AreaID = query.AreaID;

            sq.ProcureCatalogID = query.ProcureCatalogID;

            int t = 0;
            IList<HZ.Supervise.Model.Supervise> CompanyList;
            HZ.Supervise.BLL.BLL_Statistics Bll_sta = (HZ.Supervise.BLL.BLL_Statistics)HZ.Supervise.BLLFactory.IBLL_Statistics();
            if (query.CompanyType == 0)
            {
                sq.CompanyName_SC = query.CompanyName_PS;
            }
            else
            {
                sq.CompanyName_PS = query.CompanyName_PS;
            }

            strgoodsid = sq.ProcureCatalogID;

            if (!string.IsNullOrEmpty(this.txtProductName.Text.Trim()))
            {
                sq.ProductName = HZ.Fun.CheckString(this.txtProductName.Text.Trim());
            }
           
            if (!string.IsNullOrEmpty(this.txtMedicinemodel.Text.Trim()))
            {
                sq.MedicineModelName = HZ.Fun.CheckString(this.txtMedicinemodel.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtPack.Text.Trim()))
            {
                sq.Pack = HZ.Fun.CheckString(this.txtPack.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtUnit.Text.Trim()))
            {
                sq.Unit = HZ.Fun.CheckString(this.txtUnit.Text.Trim());
            }
            if (!string.IsNullOrEmpty(this.txtPurchaseName.Text.Trim()))
            {
                sq.OrderName = HZ.Fun.CheckString(this.txtPurchaseName.Text.Trim());
            }

            sq.SortFields = "ProductName ASC";
            
            sq.Append = Function.FormatPlanTime("PlanTime", query.Mid);
            if (query.SortSource ==0)
            {
                sq.Append += "and StockType=0";
            }
            if (query.SortSource == 1)
            {
                sq.Append += "and StockType=1";
            }
            if (query.SortSource == 2)
            {
                sq.Append += "and StockType=2";
            }
            sq.Append += " and OrderDetailState >0";
            sq.Mid = "";

            CompanyList = Bll_sta.GetTradeInfo(sq, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out t);
            DataTable dt = HZ.Data.DAL_Helper.GetDataTableFromModel<HZ.Supervise.Model.Supervise>(CompanyList);
            HZ.Supervise.Web.Function.ChangePSJE(dt, "DistributeCount", "Price");
            AspNetPager1.RecordCount = t;
            this.gvwDetail.DataSource = dt;
            this.gvwDetail.DataBind();

        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
        }

        /// <summary>
        /// 合并表头
        /// create by 张雄  2011.5.5
        /// </summary>
        /// <param name="sender">事件发送源</param>
        /// <param name="e">事件参数</param>
        protected void gvwDetail_RowCreated(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                case DataControlRowType.Header:
                    //取得月份
                    //第一行表头
                    TableCellCollection tc = e.Row.Cells;
                    tc.Clear();
                    tc.Add(new TableHeaderCell());
                    tc[0].Attributes.Add("colspan", "18"); //跨Column
                    tc[0].Attributes.Add("bgcolor", "LightSteelBlue");
                    SuperviseQuery sq = (SuperviseQuery)ViewState["query"];
                    tc[0].Text = ForMatDate(sq.Mid) + " " + sq.CompanyName_PS + "</th></tr><tr>";

                    //第2行表头
                    tc.Add(new TableHeaderCell());
                    tc[1].Attributes.Add("bgcolor", "Khaki");
                    tc[1].Text = "序号";

                    tc.Add(new TableHeaderCell());
                    tc[2].Attributes.Add("bgcolor", "Khaki");
                    tc[2].Text = "采购数量";

                    tc.Add(new TableHeaderCell());
                    tc[3].Attributes.Add("bgcolor", "Khaki");
                    tc[3].Text = "配送数量";

                    tc.Add(new TableHeaderCell());
                    tc[4].Attributes.Add("bgcolor", "Khaki");
                    tc[4].Text = "入库数量";

                    tc.Add(new TableHeaderCell());
                    tc[5].Attributes.Add("bgcolor", "Khaki");
                    tc[5].Text = "采购总金额";

                    tc.Add(new TableHeaderCell());
                    tc[6].Attributes.Add("bgcolor", "Khaki");
                    tc[6].Text = "入库总金额";

                    tc.Add(new TableHeaderCell());
                    tc[7].Attributes.Add("bgcolor", "Khaki");
                    tc[7].Text = "配送总金额";

                    tc.Add(new TableHeaderCell());
                    tc[8].Attributes.Add("bgcolor", "Khaki");
                    tc[8].Text = "采购单名称";

                    tc.Add(new TableHeaderCell());
                    tc[9].Attributes.Add("bgcolor", "Khaki");
                    tc[9].Text = "开始时间";

                    tc.Add(new TableHeaderCell());
                    tc[10].Attributes.Add("bgcolor", "Khaki");
                    tc[10].Text = "截至时间";

                    tc.Add(new TableHeaderCell());
                    tc[11].Attributes.Add("bgcolor", "Khaki");
                    tc[11].Text = "通用名";

                    tc.Add(new TableHeaderCell());
                    tc[12].Attributes.Add("bgcolor", "Khaki");
                    tc[12].Text = "剂型";

                    tc.Add(new TableHeaderCell());
                    tc[13].Attributes.Add("bgcolor", "Khaki");
                    tc[13].Text = "采购规格";

                    tc.Add(new TableHeaderCell());
                    tc[14].Attributes.Add("bgcolor", "Khaki");
                    tc[14].Text = "采购单位";

                    tc.Add(new TableHeaderCell());
                    tc[15].Attributes.Add("bgcolor", "Khaki");
                    tc[15].Text = "采购单价（元）";

                    tc.Add(new TableHeaderCell());
                    tc[16].Attributes.Add("bgcolor", "Khaki");
                    tc[16].Text = "生产企业";

                    tc.Add(new TableHeaderCell());
                    tc[17].Attributes.Add("bgcolor", "Khaki");
                    tc[17].Text = "配送企业";

                    tc.Add(new TableHeaderCell());
                    tc[18].Attributes.Add("bgcolor", "Khaki");
                    tc[18].Text = "采购医院";
                    break;
            }
        }

        /// <summary>
        /// 格式化日期
        /// create by 张雄  2011.5.5
        /// </summary>
        /// <param name="str">为格式化的日期参数</param>
        /// <returns>格式化后的日期</returns>
        private string ForMatDate(string str)
        {
            string YearStr = String.Empty;
            string MonthStr = String.Empty;

            if (!string.IsNullOrEmpty(str))
            {
                if (str.Length == 6)
                {
                    //获取年的值
                    YearStr = str.Substring(0, 4) + "年";
                    MonthStr = str.Substring(4, 2);     //获取月的值
                    if (MonthStr.Substring(0, 1) == "0")    //判断月 如果第一位是0 则显示一位
                    {
                        MonthStr = MonthStr.Substring(1, 1) + "月";
                    }
                    else
                    {
                        MonthStr += "月";
                    }
                }
            }
            return str = YearStr + MonthStr;
        }

        /// <summary>
        /// 分页
        /// by张雄  2011.5.8
        /// </summary>
        /// <param name="sender">时间发送源</param>
        /// <param name="e">参数</param>
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindList();
        }

        /// <summary>
        /// 导出数据
        /// </summary>
        /// <param name="sender">事件发送源</param>
        /// <param name="e">事件参数</param>
        protected void btnExcel_Click(object sender, EventArgs e)
        {
            SuperviseQuery query = ViewState["query"] as SuperviseQuery;

            strMosth = query.Mid;
            strgoodsid = query.ProcureCatalogID;
            strcompanynameps = query.CompanyName_PS;

            SuperviseQuery sq = new SuperviseQuery();
            int t = 0;
            IList<HZ.Supervise.Model.Supervise> CompanyList;
            HZ.Supervise.BLL.BLL_Statistics Bll_sta = (HZ.Supervise.BLL.BLL_Statistics)HZ.Supervise.BLLFactory.IBLL_Statistics();
            if (query.CompanyType == 0)
            {
                sq.CompanyName_SC = strcompanynameps;
            }
            else
            {
                sq.CompanyName_PS = strcompanynameps;
            }

            sq.ProcureCatalogID = strgoodsid;
            sq.Append = Function.FormatPlanTime("PlanTime", strMosth);
            if (query.SortSource == 0)
            {
                sq.Append += "and StockType=0";
            }
            if (query.SortSource == 1)
            {
                sq.Append += "and StockType=1";
            }
            if (query.SortSource == 2)
            {
                sq.Append += "and StockType=2";
            }
            sq.Append += " and OrderDetailState >0";
            sq.ProductName = HZ.Fun.CheckString(this.txtProductName.Text.Trim());
            sq.MedicineModelName = HZ.Fun.CheckString(this.txtMedicinemodel.Text.Trim());
            sq.Pack = HZ.Fun.CheckString(this.txtPack.Text.Trim());
            sq.Unit = HZ.Fun.CheckString(this.txtUnit.Text.Trim());
            sq.OrderName = HZ.Fun.CheckString(this.txtPurchaseName.Text.Trim());
            sq.SortFields = "ProductName ASC";

            CompanyList = Bll_sta.GetTradeInfo(sq, 1, 0, out t);

            DataTable dt = HZ.Data.DAL_Helper.GetDataTableFromModel<HZ.Supervise.Model.Supervise>(CompanyList);
            HZ.Supervise.Web.Function.ChangePSJE(dt, "DistributeCount", "Price");
            HZ.Excel.ExcelHelper.ExportDataForWeb(dt, this.gvwDetail);
        }
    }
}
